<?php


namespace app\admin\controller;
use app\admin\validate\AddUser;
use app\api\controller\Alipay;
use app\common\model\AccountLog;
use app\common\model\Change;
use app\common\model\DlUser;
use app\common\model\Given;
use app\common\model\JlLog;
use app\common\model\JlLqLog;
use app\common\model\Order;
use app\common\model\Recharge;
use app\common\model\UserAddress;
use app\common\model\UserLevel;
use app\common\model\UserRechargeLog;
use app\common\model\Users;
use app\common\model\UserSign;
use app\common\model\UserTdLevel;
use app\common\model\Wallet;
use app\common\model\Withdrawals;
use app\common\util\AdminException;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class User extends Base
{
    public function index(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $is_lock = input('is_lock');
        $level = input('level');
        if ($is_lock!=''){
            $where[]=['is_lock','=',$is_lock];
        }
        if ($level){
            $where[]=['level','=',$level];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $where[]=['mobile','like',"%$search_key%"];
            }else{
                $where[]=['realname','like',"%$search_key%"];
            }
        }
        $list=Users::where($where)->order('user_id desc')->paginate([
            'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'is_lock'=>$is_lock,'level'=>$level],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        $levelList=Db::name('user_level')->order('level_id')->field('level_id,name')->select();
        View::assign('levelList', $levelList);
        return view();
    }
    //导出会员
    public function export_users(){
        //搜索条件
        $ids = input('ids');
        $user_id=input('user_id');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['user_id','in', $ids];
        }else if($user_id){
            $user_ids=get_td_ids($user_id);
            $where[] = ['user_id','in', $user_ids];
        }else{
            $search_type = input('search_type');
            $search_key = input('search_key');
            $is_lock = input('is_lock');
            $level = input('level');
            if ($is_lock!=''){
                $where[]=['is_lock','=',$is_lock];
            }
            if ($level){
                $where[]=['level','=',$level];
            }
            if ($search_key){
                if ($search_type=='mobile'){
                    $where[]=['mobile','like',"%$search_key%"];
                }else{
                    $where[]=['realname','like',"%$search_key%"];
                }
            }
        }
        $userList = Users::where($where)->order('user_id desc')->select()->each(function ($item) use ($user_id){
            if ($user_id){
                $item['cengji']=get_td_cengji($item['user_id'],$user_id);
            }
            return $item;
        });
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        if ($user_id){
            $strTable .= '<td style="text-align:center;font-size:12px;">层级</td>';
        }
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">推荐人</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">粮票</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">积分</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">牧场补贴</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">储备金</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">会员等级</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">注册日期</td>';
        $strTable .= '</tr>';
        if($userList){
            foreach($userList as $val){
                if ($val['realname']){
                    $realname=$val['realname'];
                }else{
                    $realname="无";
                }
                if ($val['reid']){
                    $reid=getMobile($val['reid']);
                }else{
                    $reid="无";
                }
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['user_id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$realname.'</td>';
                if ($user_id){
                    $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['cengji'].'层</td>';
                }
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['mobile'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$reid.'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['user_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['jifen'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mcbt'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['reserve_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['dengji']['name'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['status_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['reg_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出会员");
        downloadExcel($strTable,'会员列表');
        exit();
    }
    public function do_login(){
        $user_id=input('user_id');
        session('user',get_user_info($user_id));
        $url=url('/qiantai/my')->domain(true)->build();
        $this->redirect($url);
    }
    //会员团队
    public function user_team(){
        $user_id=input('user_id');
        if (empty($user_id)){
            $this->error('会员不存在');
        }
        $user_ids=get_td_ids($user_id);
        // 搜索条件
        $where = array();
        $search_type = input('search_type');
        $search_key = input('search_key');
        $is_lock = input('is_lock');
        $level = input('level');
        if ($is_lock!=''){
            $where[]=['is_lock','=',$is_lock];
        }
        if ($level){
            $where[]=['level','=',$level];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $where[]=['mobile','like',"%$search_key%"];
            }else{
                $where[]=['realname','like',"%$search_key%"];
            }
        }
        $list=Users::where($where)->whereIn('user_id',implode(',',$user_ids))->paginate([
            'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'is_lock'=>$is_lock,'level'=>$level],
            'list_rows'=>$this->page_size
        ])->each(function ($item) use ($user_id) {
            $item['cengji']=get_td_cengji($item['user_id'],$user_id);
            return $item;
        });
        if ($user_id){
            $text="会员".getMobile($user_id)."团队列表";
        }else{
            $text="团队列表";
        }
        //清除缓存 不然text变量不生效
        $runtimepath=runtime_path();
        delDir($runtimepath."temp/");
        // 获取分页显示
        $page = $list->render();
        $levelList=Db::name('user_level')->order('level_id')->field('level_id,name')->select();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('levelList', $levelList);
        View::assign('text', $text);
        View::assign('user_id', $user_id);
        return view();
    }
    //会员详情
    public function detail(){
        $user_id=input('user_id');
        if (empty($user_id)){
            $this->error('会员不存在');
        }
        $user=Users::find($user_id);
        if (empty($user)){
            $this->error('会员不存在');
        }
        if (IS_POST){
            $data=input('post.');
            $mobile=input('mobile');
            $realname=input('realname');
            $remobile=input('remobile');
            $password=input('password');
            $conpassword=input('conpassword');
            if (empty($data['mobile'])){
                throw new AdminException('手机号码不能为空');
            }
            $count_where[]=['mobile','=',$mobile];
            $count_where[]=['user_id','<>',$user_id];
            $count=Db::name('users')->where($count_where)->count();
            if ($count){
                throw new AdminException('手机号码已注册');
            }
            if ($password || $conpassword){
                if (empty($password)){
                    throw new AdminException('新密码不能为空');
                }
                if (mb_strlen($password)<3){
                    throw new AdminException('新密码至少3位');
                }
                if (empty($conpassword)){
                    throw new AdminException('确认密码不能为空');
                }
                if ($password!=$conpassword){
                    throw new AdminException('两次密码输入不一致');
                }
                $data['password']=encrypt($data['password']);
            }else{
                unset($data['password']);
            }
            if(!empty($remobile)){
                //获取推荐人账号是否有变化
                $reuser=Users::where('mobile',$remobile)->find();
                if (empty($reuser)){
                    throw new AdminException('推荐人不存在');
                }
                if ($reuser['user_id']!=$user['reid']){
                    $td_ids = get_td_ids($user_id);
                    if(in_array($reuser['user_id'],$td_ids)){
                        throw new AdminException('推荐人不能是团队下会员');
                    }
                    if ($reuser['is_lock']==1){
                        throw new AdminException('推荐人状态异常');
                    }
                    if ($reuser['mobile']==$mobile){
                        throw new AdminException('推荐人不能为自己');
                    }
                    $data['reid'] = $reuser['user_id'];
                }
            }else{
                $data['reid'] = 0;
            }
            if ($data['level']!=$user['level']){
                if ($data['level']<$user['level']){
                    return json(['code'=>0,'msg'=>'会员等级不允许降级']);
                }
//                if ($user['valid_time']==0){
//                    $data['valid_time']=time();
//                    td_num($user['user_id']);
//                }
            }
            unset($data['conpassword']);
            $res=$user->save($data);
            if ($res){
//                if ($data['level']!=$user['level']){
//                    //升级后给推荐人调用一下升级
//                    if ($user['reid']){
//                        update_user_level($user['reid']);
//                    }
//                }
                throw new AdminException('操作成功',1,['url'=>url('User/detail',['user_id'=>$user_id])->build()]);
            }else{
                throw new AdminException('没有任何修改');
            }
        }
        $level_list=Db::name('user_level')->order('level_id')->field('level_id,name')->select();
        View::assign('level_list', $level_list);
        View::assign('user', $user);
        return view();
    }
    //会员资金记录
    public function account_log(){
        // 搜索条件
        $where = [];
        $type = input('type');
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($type!=''){
            $where[]=['type','=',$type];
        }
        if ($search_key){
            $ids=Db::name('users')->whereLike($search_type,"%$search_key%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        $list = AccountLog::where($where)
            ->order('log_id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
            'query'=>['type'=>$type,'search_type'=>$search_type,'search_key'=>$search_key],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        $text="会员资金";
        //清除缓存 不然text变量不生效
        $runtimepath=runtime_path();
        delDir($runtimepath."temp/");
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('text', $text);
        return view();
    }
    //导出资金记录
    public function export_account_log()
    {
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['log_id','in', $ids];
        }else{
            $type = input('type');
            $search_type = input('search_type');
            $search_key = input('search_key');
            if ($type!=''){
                $where[]=['type','=',$type];
            }
            if ($search_key){
                if ($search_type=='mobile'){
                    $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }else{
                    $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }
            }
        }
        $orderList = AccountLog::where($where)
            ->order('log_id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">贡献者</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">贡献者姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">变动资金</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">钱包类型</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">订单编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">说明</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">变动时间</td>';
        $strTable .= '</tr>';
        if($orderList){
            foreach($orderList as $val){
                $order_sn=$val['order_sn'] ?: "无";
                $guser_mobile=$val['gid'] ?  $val['guser']['mobile'] : "---";
                $guser_name=$val['gid'] ?  $val['guser']['realname'] : "---";
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['log_id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['mobile'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['realname'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$guser_mobile.'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$guser_name.'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['type_text'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$order_sn.'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['desc'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出会员资金");
        downloadExcel($strTable,'会员资金');
        exit();
    }
    //后台会员充值记录
    public function user_recharge_log(){
        // 搜索条件
        $where = [];
        $type = input('type');
        $wid = input('wid');
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($type!=''){
            $where[]=['type','=',$type];
        }
        if ($wid){
            $where[]=['wid','=',$wid];
        }
        if ($search_key){
            $ids=Db::name('users')->whereLike($search_type,"%$search_key%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        $list = UserRechargeLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
                'query'=>['type'=>$type,'wid'=>$wid,'search_type'=>$search_type,'search_key'=>$search_key],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        $text="充值记录";
        //清除缓存 不然text变量不生效
        $runtimepath=runtime_path();
        delDir($runtimepath."temp/");
        $wallet_list=Wallet::field("id,name")->select();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('text', $text);
        View::assign('wallet_list', $wallet_list);
        return view();
    }
    //导出后台会员充值记录
    public function export_user_recharge_log()
    {
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['id','in', $ids];
        }else{
            $type = input('type');
            $wid = input('wid');
            $search_type = input('search_type');
            $search_key = input('search_key');
            if ($type!=''){
                $where[]=['type','=',$type];
            }
            if ($wid){
                $where[]=['wid','=',$wid];
            }
            if ($search_key){
                if ($search_type=='mobile'){
                    $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }else{
                    $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }
            }
        }
        $orderList = UserRechargeLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">变动资金</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">钱包类型</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">说明</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">变动时间</td>';
        $strTable .= '</tr>';
        if($orderList){
            foreach($orderList as $val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['mobile'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['realname'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['type_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['desc'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出充值记录");
        downloadExcel($strTable,'充值记录');
        exit();
    }
    //代理会员列表
    public function dl_user_list(){
        // 搜索条件
        $where = [];
        $level = input('level');
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($level!=''){
            $where[]=['level','=',$level];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list = DlUser::where($where)->paginate([
            'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'level'=>$level],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //添加编辑代理
    public function add_edit_dl_user(){
        $id=input('id');
        $dl_user=new DlUser();
        $citys=$districts=[];
        if ($id){
            $info=$dl_user->find($id);
            $citys_where[]=['parent_id','=',$info['pid']];
            $citys=Db::name('region')->where($citys_where)->select()->toArray();
            $districts_where[]=['parent_id','=',$info['cid']];
            $districts=Db::name('region')->where($districts_where)->select()->toArray();
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\DlUser::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                throw new AdminException($values[0],10,['result'=>$error]);
            }
            $user=get_user_info($data['mobile'],1);
            $data['user_id']=$user['user_id'];
            $data['province']=Db::name('region')->where('id',$data['pid'])->value('name');
            $data['city']=Db::name('region')->where('id',$data['cid'])->value('name');
            $data['district']=Db::name('region')->where('id',$data['did'])->value('name');
            if ($id){
                $res=$info->save($data);
            }else{
                $res=$dl_user->save($data);
            }
            if ($res){
                adminLog($id?"编辑代理会员":"添加代理会员");
                throw new AdminException('操作成功',1,['url'=>url('User/dl_user_list')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        //查出来一级的
        $level1_where[]=['level','=',1];
        $level1_where[]=['parent_id','=',0];
        $parents=Db::name('region')->where($level1_where)->select()->toArray();
        View::assign('info', $info);
        View::assign('parents', $parents);
        View::assign('citys', $citys);
        View::assign('districts', $districts);
        return view();
    }
    //会员奖励记录
    public function jl_log(){
        // 搜索条件
        $where = [];
        $mobile = input('mobile');
        $level = input('level');
        if ($level){
            $where[]=['level','=',$level];
        }
        if ($mobile){
            $ids=Db::name('users')->whereLike('mobile',"%$mobile%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        $list = JlLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
            'query'=>['level'=>$level,'mobile'=>$mobile],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        $level_list=Db::name('user_level')->order('level_id')->field('level_id,name')->select();
        View::assign('level_list', $level_list);
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('mobile', $mobile);
        return view();
    }
    //导出奖励记录
    public function export_jl_log(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['id','in', $ids];
        }else{
            $mobile = input('mobile');
            $level = input('level');
            if ($level){
                $where[]=['level','=',$level];
            }
            if ($mobile){
                $ids=Db::name('users')->whereLike('mobile',"%$mobile%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $orderList = JlLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">会员等级</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">认养金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励比例</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励说明</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">创建时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">领取时间</td>';
        $strTable .= '</tr>';
        if($orderList){
            foreach($orderList as $val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['mobile'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['dengji']['name'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['amount'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['jl_bili'].'%</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['desc'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['status_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['end_time_text'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出团队分红");
        downloadExcel($strTable,'团队分红');
        exit();
    }
    //会员奖励领取记录
    public function jl_lq_log(){
        // 搜索条件
        $where = [];
        $mobile = input('mobile');
        $type = input('type');
        if ($mobile){
            $ids=Db::name('users')->whereLike('mobile',"%$mobile%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        if ($type){
            $where[]=['type','=',$type];
        }
        $list = JlLqLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
            'query'=>['mobile'=>$mobile],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('mobile', $mobile);
        return view();
    }
    //导出会员奖励领取记录
    public function export_jl_lq_log(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['id','in', $ids];
        }else{
            $mobile = input('mobile');
            $type = input('type');
            if ($mobile){
                $ids=Db::name('users')->whereLike('mobile',"%$mobile%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
            if ($type){
                $where[]=['type','=',$type];
            }
        }
        $orderList = JlLqLog::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励类型</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">奖励说明</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">创建时间</td>';
        $strTable .= '</tr>';
        if($orderList){
            foreach($orderList as $val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['mobile'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['type_text'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['desc'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出团队分红领取记录");
        downloadExcel($strTable,'分红领取');
        exit();
    }
    //资金调节
    public function account_edit(){
        $user_id=input('user_id');
        $user=Users::find($user_id);
        if (IS_POST){
            $user_money=input('user_money');
            $jifen=input('jifen');
            $mcbt=input('mcbt');
            $reserve_money=input('reserve_money');
            if (empty($user_money) && empty($jifen) && empty($mcbt) && empty($reserve_money)){
                throw new AdminException('至少变动一种资金');
            }
            $desc=input('desc');
            if (empty($desc)){
                $desc="管理调整";
            }
            $log=true;
            if ($user_money){
                $log=accountLog($user_id,$user_money,$desc);
                //插入充值记录表
                inset_recharge_log($user_id,1,0,$user_money,$desc);
            }
            if ($jifen){
                $log=accountLog($user_id,$jifen,$desc,2);
                //插入充值记录表
                inset_recharge_log($user_id,2,0,$jifen,$desc);
            }
            if ($mcbt){
                $log=accountLog($user_id,$mcbt,$desc,3);
                //插入充值记录表
                inset_recharge_log($user_id,3,0,$mcbt,$desc);
            }
            if ($reserve_money){
                $log=accountLog($user_id,$reserve_money,$desc,4);
                //插入充值记录表
                inset_recharge_log($user_id,4,0,$reserve_money,$desc);
            }
            if ($log){
                adminLog("变动会员资金");
                throw new AdminException('操作成功',1,['url'=>url('User/account_edit',['user_id'=>$user_id])->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        View::assign('user', $user);
        return view();
    }
    //添加会员
    public function add_user(){
        if (IS_POST) {
            $data = input('post.');
            try {
                validate(AddUser::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                throw new AdminException($values[0],10,['result'=>$error]);
            }
            $rekey=$data['rekey'];
            $reuser=get_user_info($rekey);
            $map['password']    =   encrypt($data['password']);
            $map['head_pic']    =   '/resource/images/default.png';
            $map['realname']    =   $data['realname'];
            $map['nickname']    =   $data['nickname'] ?: $data['mobile'];
            $map['mobile']      =   $data['mobile'];
            $map['reid']        =   $reuser?$reuser['user_id']:0;
            $map['jt_id']       =   $reuser?$reuser['reid']:0;
            $map['rekey']       =   getReKey();
            $map['token']       =   getToken();
            $map['reg_time']    =   time();
            $res=Db::name('users')->insertGetId($map);
            if ($res){
                adminLog("添加会员");
                //增加团队人数
                td_all_num($res);
                throw new AdminException('添加成功',1,['url'=>url('User/index')->build()]);
            }else{
                throw new AdminException('添加失败');
            }
        }
        return view();
    }
    //会员钱包
    public function wallet_index(){
        $where = [];
        $name = input('name');
        if ($name){
            $where[]=['name','like',"%$name%"];
        }
        $list=Wallet::where($where)->order('id desc')->paginate([
            'query'=>['name'=>$name],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //添加编辑钱包
    public function add_edit_wallet(){
        $id=input('id');
        $model=new Wallet();
        if ($id){
            $info=$model->find($id);
            View::assign('info', $info);
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\Wallet::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                throw new AdminException($values[0],10,['result'=>$error]);
            }
            if ($info){
                $res=$info->save($data);
            }else{
                $res=$model->save($data);
            }
            if ($res){
                adminLog($id?"编辑会员钱包":"添加会员钱包");
                throw new AdminException('操作成功',1,['url'=>url('User/wallet_index')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        return view();
    }
    //团队等级列表
    public function td_level_list(){
        // 搜索条件
        $where = [];
        $list=UserTdLevel::where($where)->paginate($this->page_size);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //添加编辑团队等级
    public function add_edit_td_level(){
        $level_id=input('level_id');
        $user_level=new UserTdLevel();
        if ($level_id){
            $level=$user_level->find($level_id);
            View::assign('level', $level);
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\UserTdLevel::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                throw new AdminException($values[0],10,['result'=>$error]);
            }
            if ($level){
                $res=$level->save($data);
            }else{
                $res=$user_level->save($data);
            }
            if ($res){
                adminLog($level_id?"编辑会员团队等级":"添加会员团队等级");
                throw new AdminException('操作成功',1,['url'=>url('User/td_level_list')->build()]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        $level_list=Db::name('user_td_level')->field('level_id,name')->select();
        View::assign('level_list', $level_list);
        return view();
    }
    //等级列表
    public function level_list(){
        // 搜索条件
        $where = [];
        $list=UserLevel::where($where)->paginate($this->page_size);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //添加编辑等级
    public function add_edit_level(){
        $level_id=input('level_id');
        $user_level=new UserLevel();
        $level=[];
        if ($level_id){
            $level=$user_level->find($level_id);
        }else{
            $level['lj_ry_open']=1;
            $level['td_ry_yeji_open']=1;
            $level['dsc_ry_yeji_open']=1;
            $level['update_open']=1;
            $level['fh_lj_ry_open']=1;
            $level['fh_td_ry_yeji_open']=1;
            $level['fh_dsc_ry_yeji_open']=1;
            $level['fh_update_open']=1;
        }
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\admin\validate\UserLevel::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                $result['result']=$error;
                throw new AdminException($values[0],10,$result);
            }
            if ($data['lj_ry_open']==0){
                $data['lj_ry_yeji']=0;
            }
            if ($data['td_ry_yeji_open']==0){
                $data['td_ry_yeji']=0;
            }
            if ($data['dsc_ry_yeji_open']==0){
                $data['dsc_ry_yeji']=0;
            }
            if ($data['update_open']==0){
                $data['market_num']=0;
                $data['num']=0;
                $data['num_level_id']=0;
            }
            if ($data['fh_lj_ry_open']==0){
                $data['fh_lj_ry_yeji']=0;
            }
            if ($data['fh_td_ry_yeji_open']==0){
                $data['fh_td_ry_yeji']=0;
            }
            if ($data['fh_dsc_ry_yeji_open']==0){
                $data['fh_dsc_ry_yeji']=0;
            }
            if ($data['fh_update_open']==0){
                $data['fh_market_num']=0;
                $data['fh_num']=0;
                $data['fh_num_level_id']=0;
            }
            if ($level_id){
                $res=$level->save($data);
            }else{
                $res=$user_level->save($data);
            }
            if ($res){
                adminLog($level_id?"编辑会员等级":"添加会员等级");
                $data['url']=url('user/level_list')->build();
                throw new AdminException('操作成功',1,$data);
            }else{
                throw new AdminException('操作失败');
            }
        }
        $level_list=Db::name('user_level')->field('level_id,name')->select();
        View::assign('level_list', $level_list);
        View::assign('level', $level);
        return view();
    }
    //签到列表
    public function user_sign(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list = UserSign::where($where)->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //提现申请
    public function withdrawals(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $tx_type = input('tx_type');
        $status = input('status');
        if ($tx_type!=''){
            $where[]=['tx_type','=',$tx_type];
        }
        if ($status!=''){
            $where[]=['status','=',$status];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $where[]=['realname','like',"%$search_key%"];
            }
        }
        $list = Withdrawals::where($where)->whereBetween('create_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'status'=>$status,'tx_type'=>$tx_type],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //导出提现
    public function export_with(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['id','in', $ids];
            $userList = Withdrawals::where($where)
                ->whereBetween('create_time',"$this->begin,$this->end")
                ->order('id desc')
                ->select();
        }else{
            $search_type = input('search_type');
            $search_key = input('search_key');
            $tx_type = input('tx_type');
            $status = input('status');
            if ($tx_type!=''){
                $where[]=['tx_type','=',$tx_type];
            }
            if ($status!=''){
                $where[]=['status','=',$status];
            }
            if ($search_key){
                if ($search_type=='mobile'){
                    $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }else{
                    $where[]=['realname','like',"%$search_key%"];
                }
            }
            $userList = Withdrawals::where($where)
                ->whereBetween('create_time',"$this->begin,$this->end")
                ->order('id desc')
                ->select();
        }
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">申请金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手续费</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">实际到账</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">提现方式</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">提现信息</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">申请时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">审核时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">拒绝时间</td>';
        $strTable .= '</tr>';
        if($userList){
            foreach($userList as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val->user['mobile'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['taxfee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['true_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['tx_type_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['zhanghu'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['status_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['check_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['refuse_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        adminLog("导出提现");
        downloadExcel($strTable,'提现申请');
        exit();
    }
    //处理提现申请
    public function with_update(){
        $ids=input('ids/a');
        if (empty($ids)){
            throw new AdminException('请选择数据');
        }
        $type=input('type');
        $with=new Withdrawals();
        $list=$with->select($ids);
        $num=0;
        $log=$log1=true;
        foreach ($list as $val){
            if ($val['status']==-1){
                if ($type==1){
                    if ($val['tx_type']==1){
                        $val->status=1;
                        $val->check_time=time();
                        $log=$val->save();
                    }elseif ($val['tx_type']==2){
                        //调用支付宝打款
                        if (getSysConfig('cash.zfb_transfer_auto')==1){
                            $alipay=new AliPay($this->app);
                            $result=$alipay->transfer($val['order_sn'],$val['true_money'],$val['zhifubao'],$val['realname'],'提现转账');
                            if ($result['code']==1){
                                $val->status=1;
                                $val->check_time=time();
                                $log=$val->save();
                            }else{
                                $val->status=-1;
                                $val->remark=$result['msg'];
                                $log=$val->save();
                            }
                        }else{
                            $val->status=1;
                            $val->check_time=time();
                            $log=$val->save();
                        }
                    }
                }else{
                    $val->status=2;
                    $val->refuse_time=time();
                    $log=$val->save();
                    $log1=accountLog($val['user_id'],$val['money'],'提现失败退回');
                }
                $num++;
            }
        }
        if ($log && $log1) {
            adminLog("处理提现申请");
            throw new AdminException("操作成功,共匹配".$num.'条记录!',1);
        } else {
            throw new AdminException('操作失败');
        }
    }
    //奖励发放
    public function do_jiangli(){
        $money=input('money');
        if ($money == '' || !is_numeric($money) || $money <= 0) {
            throw new AdminException('请输入正确的发放金额');
        }
        //查询团队等级列表
        $level_list_where[]=['level_id','>',1];
        $level_list_where[]=['jl_bl','=',0];
        $level_list=Db::name('user_td_level')->where($level_list_where)->field('level_id,name')->select();
        if (empty($level_list)){
            throw new AdminException('没有可发放奖励的团队等级');
        }
        foreach ($level_list as $item){
            $user_where['is_lock']=0;
            $user_where['td_level']=$item['level_id'];
            $users=Db::name('users')->where($user_where)->field('user_id')->select();
            foreach ($users as $user){
                $desc="{$item['name']}奖励";
                accountLog($user['user_id'],$money,$desc);
            }
        }
        throw new AdminException('发放成功',1);
    }
    //充值
    public function recharge(){
        // 搜索条件
        $where = [];
        $keyword = input('keyword');
        $order_status = input('order_status');
        if ($order_status!=''){
            $where[]=['order_status','=',$order_status];
        }
        if ($keyword){
            $ids=Db::name('users')->whereLike('mobile',"%$keyword%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        $list = Recharge::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['keyword'=>$keyword,'order_status'=>$order_status],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //处理会员充值
    public function recharge_update(){
        $ids=input('ids/a');
        if (empty($ids)){
            throw new AdminException('请选择数据');
        }
        $type=input('type');
        $with=new Recharge();
        $list=$with->select($ids);
        $num=0;
        $log=$log1=true;
        foreach ($list as $val){
            if ($val['order_status']==-1){
                if ($type==1){
                    $val->order_status=1;
                    $val->check_time=time();
                    $log=$val->save();
                    $log1=accountLog($val['user_id'],$val['order_amount'],'会员充值');
                }else{
                    $val->order_status=2;
                    $val->refuse_time=time();
                    $log=$val->save();
                }
                $num++;
            }
        }
        if ($log && $log1) {
            adminLog("处理会员充值");
            throw new AdminException("操作成功,共匹配".$num.'条记录!',1);
        } else {
            throw new AdminException('操作失败');
        }
    }
    //导出充值
    public function export_recharge(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $order_status = input('order_status');
        if ($order_status!=''){
            $where[]=['order_status','=',$order_status];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list = Recharge::where($where)->whereBetween('add_time',"$this->begin,$this->end")->order('id desc')->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">充值ID</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">支付方式</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">充值金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">充值状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">创建时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">支付时间</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.getMobile($val['user_id']).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val->user['realname'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['pay_name'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['order_amount'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['status_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_time_text'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出充值列表");
        downloadExcel($strTable,'会员充值');
        exit();
    }
    //转化
    public function change(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id','in',$ids];
            }
        }
        $list = Change::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //导出转化
    public function export_change(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where = [];
        if($ids){
            $where[] = ['id','in', $ids];
        }else{
            $search_type = input('search_type');
            $search_key = input('search_key');
            if ($search_key){
                if ($search_type=='mobile'){
                    $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }else{
                    $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                    $where[]=['user_id','in',$ids];
                }
            }
        }
        $list = Change::where($where)->whereBetween('add_time',"$this->begin,$this->end")->order('id desc')->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">转化金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手续费</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">实际到账</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">创建时间</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $val){
                $realname=$val->user['realname']?:'无';
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.getMobile($val['user_id']).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$realname.'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['taxfee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['true_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出转化列表");
        downloadExcel($strTable,'转化列表');
        exit();
    }
    //转赠
    public function given(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $type = input('type');
        if ($type){
            $where[]=['type','=',$type];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id|dfuid','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id|dfuid','in',$ids];
            }
        }
        $list = Given::where($where)
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['search_type'=>$search_type,'search_key'=>$search_key,'type'=>$type],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //导出转账
    public function export_given(){
        // 搜索条件
        $where = [];
        $search_type = input('search_type');
        $search_key = input('search_key');
        $type = input('type');
        if ($type){
            $where[]=['type','=',$type];
        }
        if ($search_key){
            if ($search_type=='mobile'){
                $ids=Db::name('users')->whereLike('mobile',"%$search_key%")->column('user_id');
                $where[]=['user_id|dfuid','in',$ids];
            }else{
                $ids=Db::name('users')->whereLike('realname',"%$search_key%")->column('user_id');
                $where[]=['user_id|dfuid','in',$ids];
            }
        }
        $list = Given::where($where)->whereBetween('add_time',"$this->begin,$this->end")->order('id desc')->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">对方手机号码</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">对方真实姓名</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">转赠金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手续费</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">实际扣除</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">创建时间</td>';
        $strTable .= '</tr>';
        if($list){
            foreach($list as $val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.getMobile($val['user_id']).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val->user['realname'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.getMobile($val['dfuid']).'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val->dfuser['realname'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['taxfee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['true_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($list);
        adminLog("导出转赠列表");
        downloadExcel($strTable,'转赠列表');
        exit();
    }
    //收货地址
    public function address(){
        // 搜索条件
        $where = [];
        $user_id = input('user_id');
        if ($user_id){
            $where['user_id']=$user_id;
        }
        if ($user_id){
            $text="会员".getMobile($user_id)."收货地址";
        }else{
            $text="收货地址";
        }
        //清除缓存 不然text变量不生效
        $runtimepath=runtime_path();
        delDir($runtimepath."temp/");
        $list = UserAddress::where($where)->paginate($this->page_size);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('text', $text);
        return view();
    }
}
